<?php

namespace App\Tools;

use PDF;

class PrPdf
{
    // 生成pdf
    public function makePdfByHtml($subsidiaryName, $title, $html, $type = 1)
    {
        $pdf = new \TCPDF('L');
        // 设置文档信息
        $pdf->SetCreator($subsidiaryName);
        $pdf->SetAuthor($subsidiaryName);
        $pdf->SetTitle($subsidiaryName);
        $pdf->SetSubject($subsidiaryName);
        $pdf->SetKeywords($subsidiaryName);
        $pdf->setPrintHeader(false);  //是否打印页眉
        $pdf->setPrintFooter(false);  //设置页面打印页脚
        $pdf->SetMargins(20, 0, 20);
        $pdf->setFontSubsetting(false);
        $pdf->SetFont('javiergb');
        //第一页
        $pdf->AddPage();
        $pdf->writeHTML($html);
        //输出PDF
        ob_clean();
        flush();
        if ($type == 1) {
            $pdf->Output("{$title}.pdf", 'D');
        } elseif ($type == 2) {
            $pdf->Output("{$title}.pdf", 'I');
        } elseif ($type == 3) {
            $pdf->Output(base_path() . "/storage/app/uploads/pdf/order/{$title}.pdf", 'F');
        }
    }

    public function makePdfByHtml2($html, $type = 1)
    {
        $html = file_get_contents(base_path() . '/public/static/admin/encOrder.html');
        PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(true)->save('11.pdf');
        $file = base_path() . '/public/11.pdf';
        if (file_exists($file)) {
            header("Content-type:application/octet-stream");
            $filename = basename($file);
            header("Content-Disposition:attachment;filename = " . $filename);
            header("Accept-ranges:bytes");
            header("Accept-length:" . filesize($file));
            readfile($file);
        } else {
            echo "<script>alert('文件不存在')</script>";
        }
    }


    public function makePdf()
    {
        $html = file_get_contents(base_path() . '/public/static/admin/encOrder.html');
        PDF::loadHTML($html)->setPaper('a4', 'landscape')->setWarnings(true)->save('11.pdf');
        $file = base_path() . '/public/11.pdf';
        if (file_exists($file)) {
            header("Content-type:application/octet-stream");
            $filename = basename($file);
            header("Content-Disposition:attachment;filename = " . $filename);
            header("Accept-ranges:bytes");
            header("Accept-length:" . filesize($file));
            readfile($file);
        } else {
            echo "<script>alert('文件不存在')</script>";
        }

    }

    public function makePdf2()
    {
        $pdf = new \TCPDF('L');
        // 设置文档信息
        $pdf->SetCreator('懒人开发网');
        $pdf->SetAuthor('懒人开发网');
        $pdf->SetTitle('TCPDF示例');
        $pdf->SetSubject('TCPDF示例');
        $pdf->SetKeywords('TCPDF, PDF, PHP');
        $pdf->setPrintHeader(false);  //是否打印页眉
        $pdf->setPrintFooter(false);  //设置页面打印页脚
        $pdf->SetMargins(40, 0, 40);
        $pdf->setFontSubsetting(false);
        $pdf->SetFont('javiergb');
        //第一页
        $pdf->AddPage();
        $html = file_get_contents(base_path() . '/public/static/admin/encOrder.html');
        $pdf->writeHTML($html);
//        $pdf->writeHTML($html);
        //输出PDF
        $pdf->Output('t1.pdf', 'D');
    }

}
